﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.ProviderLoader.Configuration;
using System.Configuration;
using PSE.Framework.ProviderLoader;

namespace PSE.ProcessManager.ResourceAccess
{
    public class ProcessManagerResourceAccessFactory
    {
        private const string CONFIG_SECTION_NAME = "providerConfiguration";
        private const string PROVIDER_NAME = "processManagerResourceAccess";

        public static IProcessManagerResourceAccess CreateProvider()
        {
            try
            {
                ProviderConfigurationSection configSection = System.Configuration.ConfigurationManager.GetSection(CONFIG_SECTION_NAME) as ProviderConfigurationSection;

                if (configSection != null)
                {
                    return ProviderLoader<IProcessManagerResourceAccess>.LoadProvider(configSection.Providers, PROVIDER_NAME);
                }
                else
                {
                    throw new ArgumentNullException("processManagerResourceAccess");
                }
            }
            catch (Exception ex)
            {
                //System.Diagnostics.EventLog.WriteEntry("ResourceAccessFactory", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                throw new ConfigurationErrorsException("Could not create an instance of Provider configured.", ex);
            }
        }
    }
}
